﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace TransformeXmlEnCsv
{
	public class Blueprint 
	{
		#region Propriétés.
		/// <summary>
		/// ID unique du Blueprint
		/// </summary>
		public int IdBluePrint { get; set; }

		/// <summary>
		/// Nom du BluePrint
		/// </summary>
		public string NomBluePrint { get; set; }

		/// <summary>
		/// Truc que j'en sais rien
		/// </summary>
		public string Metagroup { get; set; }

		/// <summary>
		/// ID du produit fabriqué par le BluePrint
		/// </summary>
		public int ProductTypeId { get; set; }

		/// <summary>
		/// Temps de production en seconde
		/// </summary>
		public int ProductionTime { get; set; }

		/// <summary>
		/// Niveau de technologie
		/// </summary>
		public short TechLevel { get; set; }

		/// <summary>
		/// Temps de recherche pour la productivité en seconde
		/// </summary>
		public int ResearchProductivityTime { get; set; }

		/// <summary>
		/// Temps de recherche pour consommer moins de matériel
		/// </summary>
		public int ResearchMaterielTime { get; set; }

		/// <summary>
		/// Temps pour faire une copie
		/// </summary>
		public int ResearchCopyTime { get; set; }

		/// <summary>
		/// Coefficient de production
		/// </summary>
		public int ProductivityModifier { get; set; }

		/// <summary>
		/// Facteur de waste (déchet)
		/// </summary>
		public int WasteFactor { get; set; }

		/// <summary>
		/// Nombre de production possible par BluePrint
		/// </summary>
		public int MaxProductionLimit { get; set; }

		/// <summary>
		/// ID du Blueprint qui peut inventer.
		/// </summary>
		public int InventTypeID { get; set; }

		/// <summary>
		/// Liste de composant pour la réalisation du BluePrint.
		/// </summary>
		public List<Composant> ListeComposants = new List<Composant>();
		

		public List<Invention> ListeInvention = new List<Invention>();

		#endregion

		

		/// <summary>
		/// Réécriture de la méthode ToString().
		/// </summary>
		public override string ToString()
		{
			const char VIRGULE = ',';
			StringBuilder sb = new StringBuilder(24);
			sb.Append(this.IdBluePrint);
			sb.Append(VIRGULE);
			sb.Append(this.NomBluePrint);
			sb.Append(VIRGULE);
			sb.Append(this.Metagroup);
			sb.Append(VIRGULE);
			sb.Append(this.ProductTypeId);
			sb.Append(VIRGULE);
			sb.Append(this.ProductionTime);
			sb.Append(VIRGULE);
			sb.Append(this.TechLevel);
			sb.Append(VIRGULE);
			sb.Append(this.ResearchProductivityTime);
			sb.Append(VIRGULE);
			sb.Append(this.ResearchMaterielTime);
			sb.Append(VIRGULE);
			sb.Append(this.ResearchCopyTime);
			sb.Append(VIRGULE);
			sb.Append(this.ProductivityModifier);
			sb.Append(VIRGULE);
			sb.Append(this.WasteFactor);
			sb.Append(VIRGULE);
			sb.Append(this.MaxProductionLimit);
			sb.Append('|');

			return sb.ToString();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="elementBlueprint"></param>
		public Blueprint(XElement elementBlueprint)
		{
			// Exemple :
			// id="29042" name="Capital Crystalline Carbonide Armor Plate Blueprint" icon="03_02" metaGroup="T1" productTypeID="29041" 
			// productionTime="750" techLevel="2" researchProductivityTime="100000" researchMaterialTime="100000" researchCopyTime="7500000" 
			// researchTechTime="90000" productivityModifier="150" wasteFactor="10" maxProductionLimit="1000">

			// Cherchons les informations dans les attributs de l'élément.
			var infoBp = (from dd in elementBlueprint.Attributes()
						  select dd);

			NomBluePrint = infoBp.Where(y => y.Name == "name").Select(x => x.Value).First();
			IdBluePrint = Convert.ToInt32(infoBp.Where(y => y.Name == "id").Select(x => x.Value).First());
			Metagroup = infoBp.Where(y => y.Name == "metaGroup").Select(x => x.Value).First();
			ProductTypeId = Convert.ToInt32(infoBp.Where(y => y.Name == "productTypeID").Select(x => x.Value).First());
			ProductionTime = Convert.ToInt32(infoBp.Where(y => y.Name == "productionTime").Select(x => x.Value).First());
			TechLevel = Convert.ToInt16(infoBp.Where(y => y.Name == "techLevel").Select(x => x.Value).First());
			ResearchProductivityTime = Convert.ToInt32(infoBp.Where(y => y.Name == "researchProductivityTime").Select(x => x.Value).First());
			ResearchMaterielTime = Convert.ToInt32(infoBp.Where(y => y.Name == "researchMaterialTime").Select(x => x.Value).First());
			ResearchCopyTime = Convert.ToInt32(infoBp.Where(y => y.Name == "researchCopyTime").Select(x => x.Value).First());
			ProductivityModifier = Convert.ToInt32(infoBp.Where(y => y.Name == "productivityModifier").Select(x => x.Value).First());
			WasteFactor = Convert.ToInt32(infoBp.Where(y => y.Name == "wasteFactor").Select(x => x.Value).First());
			MaxProductionLimit = Convert.ToInt32(infoBp.Where(y => y.Name == "maxProductionLimit").Select(x => x.Value).First());
			

			// Pour chaque composants.
			// ActivityID = 1 --> Manufacture , ActivityID = 8 --> Invention.
			foreach (var item in elementBlueprint.Descendants("m")
												 .Where(y => y.Attribute("activityId").Value == "8" || y.Attribute("activityId").Value == "1")
												 .Select(x => x))
			{
				ListeComposants.Add(new Composant(item, IdBluePrint));
			}


			foreach (var elementInvention in elementBlueprint.Descendants("inventTypeID"))
			{
				ListeInvention.Add(new Invention(IdBluePrint, Convert.ToInt32(elementInvention.Value)));
			}

		}

	}
}
